#include "collectiongrabwindow.h"
#include <QDesktopWidget>
#include <QBuffer>
#include <QApplication>
#include <QString>
#include <QScreen>
#include <QPixmap>
#include <QImage>
#include <unistd.h>

collectionGrabWindow::collectionGrabWindow()
{
    m_bCollection=false;
}
bool  collectionGrabWindow::collection()
{    
    bool ret=false;
    QDesktopWidget * des = QApplication::desktop();
        QScreen *screen =QApplication::primaryScreen();
        if (screen)
        {
            m_Pixmap = screen->grabWindow(des->winId(),0,0,des->width(),des->height());
            if(!m_Pixmap.isNull())
                ret=true;
        }
      return ret;
}
bool collectionGrabWindow::outBMP(QString filename)
{
    bool ret=false;    
    if(collection()){
        ret=m_Pixmap.save(filename,"BMP");
    }
    return ret;
}
bool collectionGrabWindow::outJPG(QString filename)
{
    bool ret=false;    
    if(collection())
    {
        QImage img=m_Pixmap.toImage();
        ret=img.save(filename,"JPEG");
    }
    return ret;
}
bool collectionGrabWindow::outPNG(QString filename)
{
    bool ret=false;
   if(collection()){
        ret=m_Pixmap.save(filename,"PNG");
    }
    return ret;
}

bool collectionGrabWindow::toByte(QByteArray &myArray)
{
        if(collection())
        {
            QBuffer buffer(&myArray);
            if(buffer.open(QIODevice::WriteOnly))
             return m_Pixmap.save(&buffer,"JPEG");

        }
    return false;
}

bool collectionGrabWindow::toMinByte07(QByteArray &myArray)
{
        if(collection())
        {
            QPixmap tmpPix=m_Pixmap;
            tmpPix=tmpPix.scaledToWidth(m_Pixmap.width()*0.7, Qt::SmoothTransformation);
            tmpPix=tmpPix.scaledToHeight(m_Pixmap.height()*0.7, Qt::SmoothTransformation);
            QBuffer buffer(&myArray);
            if(buffer.open(QIODevice::WriteOnly))
                return tmpPix.save(&buffer,"JPEG");
        }
    return false;
}
bool collectionGrabWindow::isCollection()
{
    return m_bCollection;
}
void collectionGrabWindow::setCollection(bool b)
{
    m_bCollection=b;
}
